/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

//
// includes
//
#include "logtablemodel.h"
#include <QFont>
#include <QColor>
#include <math.h>

//
// FIXME: too frequent updates - inefficient
//

/*
================
data
================
*/
QVariant LogTableModel::data( const QModelIndex &index, int role ) const {
    LogEntry *lPtr;
    PrecursorEntry *pPtr = NULL;
    QString base;

    if ( !index.isValid())
        return QVariant();

    if ( index.row() >= m.logListFiltered.count() || index.row() < 0 )
        return QVariant();

    // get selected log
    lPtr = m.logListFiltered.at( index.row());
    if ( lPtr == NULL )
        return QVariant();

    // display remaining in bold
    if ( role == Qt::FontRole ) {
        if ( index.column() == Remaining ) {
            QFont font;
            font.setBold( true );
            return font;
        }
    }
    else if ( role == Qt::BackgroundRole ) {
        // highlight additions/restock
        if ( lPtr->amount() > 0 )
            return QColor::fromRgbF( 0.0f, 1.0f, 0.0f, 0.2f );
    } // display cell data
    else if ( role == Qt::DisplayRole ) {
        switch ( index.column()) {
        case Date:
            return lPtr->date();

        case Purpose:
            return lPtr->purpose();

        case Amount:
            // find corresponding precursor
            pPtr = m.precursorForId( lPtr->precursorId());
            if ( pPtr == NULL )
                return 0;

            // make base string
            base = QString( "%1 %2" )
                    //.arg( Main::setPrecision( fabs( lPtr->amount()), ))
                    .arg( QString::number( fabs( lPtr->amount()), 'f', Main::getPrecision( lPtr->units())))
                    .arg( Common::UnitsStrings[lPtr->units()]);

            // highlight additions/restock
            if ( lPtr->amount() > 0 )
                base.prepend( "+ ");

            // display both original and converted units if necessary
            if ( pPtr->units() == lPtr->units())
                return base;
            else
                return QString( "%1\n(%2 %3)" )
                        .arg( base )
                        //.arg( Main::setPrecision( fabs( lPtr->amountTo( pPtr->units(), pPtr->density())), Main::getPrecision( pPtr->units())))
                        .arg( QString::number( fabs( lPtr->amountTo( pPtr->units(), pPtr->density())), 'f', Main::getPrecision( pPtr->units())))
                        .arg( Common::UnitsStrings[pPtr->units()]);
            break;

        case Remaining:
            // find corresponding precursor
            pPtr = m.precursorForId( lPtr->precursorId());
            if ( pPtr == NULL )
                return 0;

            return QString( "%1 %2" ).arg( QString::number( m.remaining( lPtr->id()), 'f', Main::getPrecision( pPtr->units()))).arg( Common::UnitsStrings[pPtr->units()]);

        default:
            return QVariant();
        }
    } else if ( role == Qt::TextAlignmentRole ) {
        return Qt::AlignCenter;
    } else if ( role == Qt::UserRole ) {
        return lPtr->id();
    }

    return QVariant();
}

/*
================
headerData
================
*/
QVariant LogTableModel::headerData( int section, Qt::Orientation orientation, int role ) const {
    if ( role != Qt::DisplayRole )
        return QVariant();

    if ( orientation == Qt::Horizontal ) {
        switch ( section ) {
        case Date:
            return this->tr( "Date" );

        case Purpose:
            return this->tr( "Purpose" );

        case Amount:
            return this->tr( "Amount" );

        case Remaining:
            return this->tr( "Remaining" );

        default:
            return QVariant();
        }
    }
    return QVariant();
}

/*
================
flags
================
*/
Qt::ItemFlags LogTableModel::flags( const QModelIndex &index ) const {
    if ( !index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractTableModel::flags( index );
}
